from django.template.loader import render_to_string
from django import template
from garetjax.events.models import Event

register = template.Library()

def render_calendar(parser, token):
    try:
        template_name = '"events/month.html"'
        tag_name, month = token.split_contents()
    except ValueError:
        try:
            template_name = '"events/month.html"'
            tag_name, month = token.split_contents()
        except ValueError:
            raise template.TemplateSyntaxError, "%r tag requires one ore two arguments" % token.contents.split()[0]
    
    return CalendarRenderer(month, template_name)

class CalendarRenderer(template.Node):
    def __init__(self, month, template_name):
        self.month = template.Variable(month)
        self.template_name = template.Variable(template_name)
        
    def render(self, context):
        request = template.Variable('request').resolve(context)
        
        try:
            template_name = self.template_name.resolve(context)
            month = self.month.resolve(context)
            
            import calendar
            
            cal = calendar.Calendar()
            weeks = cal.monthdays2calendar(month.year, month.month)
            
        except (template.VariableDoesNotExist):
            return ''
            
        c = template.RequestContext(request, {
            'month': month,
            'weeks': weeks,
        })
        
        return render_to_string(template_name, c)
        
register.tag('calendar', render_calendar)

def get_events(parser, token):
    try:
        tag_name, year, month, day = token.split_contents()
    except ValueError:
        try:
            day = None
            tag_name, year, month = token.split_contents()
        except ValueError:
            raise template.TemplateSyntaxError, "%r tag requires at least two arguments" % token.contents.split()[0]
    
    return SetEvents(year, month, day)

class SetEvents(template.Node):
    def __init__(self, year, month, day):        
        self.year = template.Variable(year)
        self.month = template.Variable(month)
        
        try:
            self.day = template.Variable(day)
        except TypeError:
            self.day = None
        
    def render(self, context):
        month = self.month.resolve(context)
        year = self.year.resolve(context)
        
        events = Event.objects.filter(date_start__month=month)\
                              .filter(date_start__year=year)

        try:
            day = self.day.resolve(context)
            events = events.filter(date_start__day=day)
        except AttributeError:
            pass
        context['events'] = events
        return ''
        
register.tag('events', get_events)

def get_events_count(parser, token):
    try:
        tag_name, year, month, day = token.split_contents()
    except ValueError:
        try:
            day = None
            tag_name, year, month = token.split_contents()
        except ValueError:
            raise template.TemplateSyntaxError, "%r tag requires at least two arguments" % token.contents.split()[0]
    
    return SetEventsCount(year, month, day)

class SetEventsCount(template.Node):
    def __init__(self, year, month, day):        
        self.year = template.Variable(year)
        self.month = template.Variable(month)
        
        try:
            self.day = template.Variable(day)
        except TypeError:
            self.day = None
        
    def render(self, context):
        month = self.month.resolve(context)
        year = self.year.resolve(context)
        
        events = Event.objects.filter(date_start__month=month)\
                              .filter(date_start__year=year)

        try:
            day = self.day.resolve(context)
            events = events.filter(date_start__day=day)
        except AttributeError:
            pass
            
        context['events_count'] = events.count()
        return ''

register.tag('events_count', get_events_count)